package examples
{
	import com.wskeee.ec.components.Button;
	import com.wskeee.ec.components.Label;
	import com.wskeee.ec.components.TextInput;
	import com.wskeee.ec.components.tree.Tree;
	import com.wskeee.ec.containers.Container;
	import com.wskeee.ec.containers.HBox;
	import com.wskeee.ec.containers.VBox;
	
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class TreeExample
	{
		public static function createTree():Container
		{
			var mainBox:VBox = new VBox();
			var head:Label = new Label();
			head.text = "Tree";
			mainBox.verticalGap = 20;
			mainBox.paddingTop = mainBox.paddingBottom = mainBox.paddingLeft = mainBox.paddingRight = 5;
			mainBox.addChild(head);
			
			//---------------------
			//XML dataProvider
			//---------------------
			var treedata:XML = <root label="root">
															<node label="XML_nodeA">
																<node label="XML_nodeA_A"/>
																<node label="XML_nodeA_B"/>
																<node label="XML_nodeA_C"/>
																<node label="XML_nodeA_D"/>
															</node>
															<node label="XML_nodeB">
																<node label="XML_nodeB_A"/>
																<node label="XML_nodeB_B"/>
															</node>
															<node label="XML_nodeC">
																<node label="XML_nodeC_A"/>
																<node label="XML_nodeC_B"/>
															</node>
															<node label="XML_nodeD"/>
													   </root>
			
			var xmlItem:XML;
			function createTreeByXML(value:int):void
			{
				treedata = <root label="tree root"/>
				for(var z:int=0;z<value;z++)
				{
					xmlItem = getXMLItem("XML_");
					if(Math.random()>0.5)
						xmlItem.appendChild(getXMLItem("XML_node"));
					treedata.appendChild(xmlItem);
				}
				tree.dataProvider = treedata;
			}
			
			function getXMLItem(name:String):XML
			{
				var xmlItem:XML = <node />;
				xmlItem.@label = name+Math.round(Math.random()*10000);
				return xmlItem;
			}
			//---------------------
			//Object dataProvider
			//---------------------
			var datas:Object = {children:[]};
			var dataItem:Object;
			function createTreeByObject(value:int):void
			{
				datas = {label:"tree root",children:[]};
				for(var i:int=0;i<value;i++)
				{
					dataItem = getObjectItem();
					dataItem.children = [getObjectItem(),getObjectItem(),getObjectItem()];
					if(Math.random()>0.5)
						dataItem.children[2].children = [getObjectItem(),getObjectItem()];
					datas.children.push(dataItem);
				}
				tree.itemHeight = 20;
				tree.dataProvider = datas;
				tree.selectedIndex = -1;
			}
			function getObjectItem():Object
			{
				return {label:"wskeee"+Math.round(Math.random()*100000000)};
			}
			
			var tree:Tree = new Tree();
			tree.dataProvider = datas;
			tree.width = 200;
			tree.height = 200;
			tree.showRoot = false;
			tree.allowMultipleSelection = true;
			tree.addEventListener(Event.CHANGE,function(evt:Event):void
			{
				infoLabel.text = "选择Item Label："+(tree.selectedItem ? tree.selectedItem.label : "");
			});
			
			mainBox.addChild(tree);
			var infoLabel:Label = new Label();
			infoLabel.text = "选择Item Label：";
			mainBox.addChild(infoLabel);
			//---------------------
			//control
			//---------------------
			//XML
			var controlBox:HBox = new HBox();
			var button:Button = new Button();
			button.label = "datafromXML";
			button.addEventListener(MouseEvent.CLICK,function(evt:MouseEvent):void
			{
				createTreeByXML(int(listTxt.text));
			});
			controlBox.addChild(button);
			
			//Object
			button = new Button();
			button.label = "datafromObject";
			button.addEventListener(MouseEvent.CLICK,function(evt:MouseEvent):void
			{
				createTreeByObject(int(listTxt.text));
			});
			controlBox.addChild(button);
			
			var listTxt:TextInput = new TextInput("4");
			listTxt.toolTip = "输入需要创建tree列表数目"
			listTxt.width = 20;
			controlBox.addChild(listTxt);
			//ShowRoot
			button = new Button();
			button.label = "showRoot";
			button.toggle = true;
			button.addEventListener(MouseEvent.CLICK,function(evt:MouseEvent):void
			{
				tree.showRoot = button.selected;
			});
			controlBox.addChild(button);
			mainBox.addChild(controlBox);
			
			createTreeByObject(4);
			
			return mainBox;
		}
	}
}